//
//  ChooseView.m
//  ChooseDemo
//
//  Created by pro on 2021/11/9.
//

#import "ChooseView.h"

@implementation ChooseView

#define kWindowH   [UIScreen mainScreen].bounds.size.height //应用程序的屏幕高度
#define kWindowW    [UIScreen mainScreen].bounds.size.width  //应用程序的屏幕宽度

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    return self;
}
-(void)setAllDataArray:(NSArray *)allDataArray
{
    _allDataArray = allDataArray;
    NSLog(@"%@",allDataArray);
    for (int i = 0; i<allDataArray.count; i++) {
        NSString *titleStr = [NSString stringWithFormat:@"%@",allDataArray[i][0]];
        TitleButton *btn = [TitleButton buttonWithType:normal];
        [btn setFrame:CGRectMake(10+(kWindowW-allDataArray.count*10)/allDataArray.count*i+10, 5, (kWindowW-allDataArray.count*10-30)/allDataArray.count, 35)];
        btn.tag = i;
        [btn.titleL setText:titleStr];
        [btn addTarget:self action:@selector(onClickButton:) forControlEvents:UIControlEventTouchUpInside];
//        [self.selectArray addObject:titleStr];
        [self.dicArray setValue:titleStr forKey:[NSString stringWithFormat:@"str%ld",i]];
        
        
        [self addSubview:btn];
    }
}
-(void)addTableviewDataArray:(NSArray *)array{
    self.table.dataArray = array;
    self.table.didBlock = ^(NSIndexPath *indexPath){
        [onBtn.titleL setText:[NSString stringWithFormat:@"%@",array[indexPath.row]]];
        [_dicArray setValue:array[indexPath.row] forKey:[NSString stringWithFormat:@"str%ld",onBtn.tag]];
        if ([_delegate respondsToSelector:@selector(chooseTableviewSelectDictionary:)]) {
            [_delegate chooseTableviewSelectDictionary:_dicArray];
        }
    };
}
-(void)onClickButton:(TitleButton *)btn{
    if (self.table.hidden==NO&&onBtn.tag == btn.tag) {
        self.table.hidden = YES;
    }else{
        onBtn = btn;
        NSArray *all = self.allDataArray[btn.tag];
        CGFloat height = all.count *45;
        if (height >= 180){
            height = 180;
        }
        self.table.hidden = NO;
        self.table.frame = CGRectMake(0, 45, kWindowW,height);
        [self addTableviewDataArray:all];
        [self setHeight:height+45];
    }
}
/**
-(NSMutableArray *)selectArray
{
    if (!_selectArray) {
        _selectArray = [NSMutableArray array];
    }
    return _selectArray;
}
*/
-(NSMutableDictionary *)dicArray
{
    if (!_dicArray) {
        _dicArray = [NSMutableDictionary dictionary];
    }
    return _dicArray;
}
-(ChooseTableview *)table{
    if (!_table) {
        _table = [[ChooseTableview alloc] init];
        [self addSubview:_table];
    }
    return _table;
}
@end
